using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

/****************************************************
// 功能：游戏启动入口
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/2/28 13:20:3
*****************************************************/

public class GameRoot : SingletonPatternMonoBase<GameRoot>
{
    private ResourceManager resource;
    private ModelManager model;
    private NetManager net;
    private AudioManager audioManager;
    private readonly List<WindowConfig> list = new List<WindowConfig>();

    public List<MonoTimer> tempTimerList = new List<MonoTimer>();
    public List<MonoTimer> timerList = new List<MonoTimer>();
    
    private void Awake()
    {
#if UNITY_IOS || UNITY_ANDROID
        if (Application.isMobilePlatform)
        {
            Handheld.PlayFullScreenMovie("opening.mp4", Color.black, FullScreenMovieControlMode.Hidden, FullScreenMovieScalingMode.Fill);
        }
#endif

        // 初始化平台消息
        //PlatformMsgManager.Instance.Init();

        // 注册服务器连接成功事件监听
        //EventDispatcher.Instance.Register(Constants.CONNECTION_REFUSED, OnConnectionRefused);

        // 添加资源加载管理器组件
        resource = gameObject.AddComponent<ResourceManager>();
        // 添加数据模型组件
        model = gameObject.AddComponent<ModelManager>();
        // 添加网络协议组件
        net = gameObject.AddComponent<NetManager>();
        // 添加音频组件
        audioManager = gameObject.AddComponent<AudioManager>();
        
        // 加载模拟战斗组件
        gameObject.AddComponent<SimulateController>();
    }

    // Start is called before the first frame update
    private void Start()
    {
        // 不需要销毁场景
        DontDestroyOnLoad(this);
        //DontDestroyOnLoad(Camera.main);
        //resource.LoadPrefab("Prefab/ScreenTapFx", transform, true);

        // 设置向左横屏
        Screen.orientation = ScreenOrientation.LandscapeLeft;

        InitUIManager();
        
        InitManagerController();

        Debug.Log("Game Start");
    }
    
    private void Update()
    {
        // 这样写是为了保证每个技能的计时器能独立运行 线程安全
        if(tempTimerList.Count > 0) {
            timerList.AddRange(tempTimerList);
            tempTimerList.Clear();
        }

        for (var i = timerList.Count - 1; i >= 0; i--)
        {
            var timer = timerList[i];
            timer.Tick(Time.deltaTime);
        }
    }

    /// <summary>
    /// 初始化UI管理器
    /// </summary>
    private void InitUIManager()
    {
        TextAsset asset = Resources.Load<TextAsset>("WebConfig");
        string[] s = asset.text.Split(new[] { "\n" }, StringSplitOptions.None);
        s = s.Where(tempS => !string.IsNullOrEmpty(tempS)).ToArray();

        foreach (var item in s)
        {
            WindowConfig windowConfig = JsonUtility.FromJson<WindowConfig>(item);
            list.Add(windowConfig);
        }
        
        UIManager.Instance.Init(transform.Find("WindowRoot"), list);
    }

    /// <summary>
    /// 初始化
    /// </summary>
    private void InitManagerController()
    {
        // Manager Init
        resource.InitManager();
        model.InitManager();
        net.InitManager();
        audioManager.InitManager();


        // Controller Init
        var login = LoginController.Instance;
        login.InitController();
        login.EnterLogin();

        LobbyController.Instance.InitController();
        MatchController.Instance.InitController();
        RoomController.Instance.InitController();
        HeroController.Instance.InitController();
        BattleController.Instance.InitController();
    }
    
    /// <summary>
    /// 在游戏主入口运行通用版计时器
    /// </summary>
    /// <param name="timer"></param>
    public void AddMonoTimer(MonoTimer timer) {
        tempTimerList.Add(timer);
    }
}